package org.devf.fork.imsimulator.view;

import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.devf.fork.imsimulator.HelloApplication;
import org.devf.fork.imsimulator.service.TokenMgr;

import java.io.IOException;

/**
 * BaseController related
 *
 * @author wanghuafu
 * @since 1.0  2023/9/15 15:47
 */
public abstract class BaseController {

    protected void refreshView(Runnable runnable) {
        // 更新UI，通过Platform.runLater()在JavaFX主线程上执行更新操作
        Platform.runLater(runnable);
    }

    protected void toChatController() throws IOException {
        FXMLLoader loader = new FXMLLoader(HelloApplication.class.getResource("chat-view.fxml"));
        Parent loginRoot = loader.load();
        Scene scene = new Scene(loginRoot);
        scene.getStylesheets().add(HelloApplication.class.getResource("chat.css").toExternalForm());
        Stage stage = new Stage();
        stage.setOnCloseRequest(event -> Platform.exit());
        stage.setScene(scene);
        stage.setTitle(TokenMgr.userModel.getNikname() + "的聊天室");
        stage.show();
    }

}
